#include<bits/stdc++.h>
using namespace std;
#define int long long
int T,n,a[200005],pre[200005],dp[200005],lst[1000005];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin>>T;
    while(T--){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i];pre[i]=pre[i-1];
            if(a[i]==a[i-1])pre[i]+=a[i];
            dp[i]=0;
        }
        memset(lst,0,sizeof(lst));
        int D=0,MAXX=0;lst[a[1]]=1;
        for(int i=2;i<=n;i++){
            D+=pre[i]-pre[i-1];
            if(lst[a[i]]!=0&&lst[a[i]]!=i-1)dp[i]=max(dp[lst[a[i]]+1]+a[i]+pre[i]-pre[lst[a[i]]],MAXX+D);
            else dp[i]=MAXX+D;
            lst[a[i]]=i;
            MAXX=max(MAXX,dp[i]-D);
        }
        int ans=0;
        for(int i=1;i<=n;i++)ans=max(ans,dp[i]+pre[n]-pre[i]);
        cout<<ans<<endl;
    }
    return 0;
}